/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.sigmatrix.h5.dao.DisDealerAcctDao;

import com.sigmatrix.h5.entity.DisDealerAcct;
import com.sigmatrix.h5.exception.DaoException;
import com.sigmatrix.h5.utils.CollectionUtil;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.utils.Utils;
import com.sigmatrix.sc.jdbc.common.dao.DaoImpl;

/**
 *@ClassName: DisDealersAcctDaoImpl
 *@Description: 经销商账号
 *@author MH
 *@date 2017年8月21日 下午9:35:22
 */
@Repository
public class DisDealerAcctDaoImpl extends DaoImpl<DisDealerAcct,Integer> implements DisDealerAcctDao{

	@Override
	public int selectCountByAccount(String account) {
		DisDealerAcct acct = new DisDealerAcct();
		acct.setAccount(account);
		return selectCount(acct);
	}

	@Override
	public DisDealerAcct findByAccount(String account) {
		DisDealerAcct acct = new DisDealerAcct();
		acct.setAccount(account);
		return selectOne(acct);
	}

	@Override
	public DisDealerAcct findSalesMan(Integer id) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from ").append(tableName()).append(" a ");
		sql.append("where a._promotion_code =(");
		sql.append("select b._dealers_acct_promotion_code from ").append(tableName()).append(" b ");
		sql.append("where b._id = ?)");
		List<DisDealerAcct> list = query(sql.toString(), id);
		return CommonUtils.isEmpty(list)?null:list.get(0);
	}
	
	
	/**
	 * 分销app合并分支开始-------------------------------------
	 */
	 

	private static final String TABLE_NAME = "_distribution_dealers_acct";

	
	/**
	 * (non-Javadoc)
	 *@Title: findById
	 *@Description: 
	 *@see com.sigmatrix.h5.dao.DisDealersAcctDao#findById(java.util.Map, java.lang.Integer)
	 *@author 
	 *@date 2017年8月14日 下午11:36:33
	 */
	@Override
	public DisDealerAcct findById(final String eseCode,final Integer id) throws DaoException {
		final StringBuilder sql = new StringBuilder()
		.append("SELECT * FROM ")
		.append(getTname(null, TABLE_NAME))
		.append(" WHERE _id=").append(id);
		logger.info(sql.toString());
		return this.findOne(sql.toString());
	}
	
	/**
	 * (non-Javadoc)
	 *@Title: findByAcct
	 *@Description: 
	 *@see com.sigmatrix.h5.dao.DisDealersAcctDao#findByAcct(java.lang.String, java.lang.String)
	 *@author 
	 *@date 2017年8月15日 上午12:18:04
	 */
	public DisDealerAcct findByAcct(final String eseCode,final String account) throws DaoException {
		final StringBuilder sql = new StringBuilder()
		.append(" SELECT * FROM ")
		.append(getTname(null, TABLE_NAME))
		.append(" WHERE _status = ").append(Cons.STATUS_ENABLE)
		.append(" AND _account = '").append(account).append("'");
		logger.info(sql.toString());
		return this.findOne(sql.toString());
			
	}
	
	/**
	 *@Title: findOne
	 *@Description: 查询一条记录
	 *@return DisDealersAcctVo
	 *@date 2017年8月14日 下午11:41:12
	 *@param sql
	 */
	private DisDealerAcct findOne(final String sql){
		final List<DisDealerAcct> list = this.finalList(sql);
		if (CollectionUtil.isNotEmpty(list)) {
			return list.get(0);
		} else {
			return null;
		} 
	}
	
	/**
	 *@Title: finalList
	 *@Description: 查询多条记录
	 *@return List<DisDealersAcctVo>
	 *@date 2017年8月14日 下午11:40:11
	 *@param sql
	 */
	private List<DisDealerAcct> finalList(final String sql){
		return jdbcTemplate.query(sql.toString(), new ObjRowMapper());
	}
	

	/**
	 *@ClassName: ObjRowMapper
	 *@Description: 封装返回的实体
	 *@author 
	 *@date 2017年8月14日 下午11:45:39
	 */
	public class ObjRowMapper implements RowMapper<DisDealerAcct> {
		public DisDealerAcct mapRow(final ResultSet rs, final int rowNum) throws SQLException {
			final DisDealerAcct obj = new DisDealerAcct();
			obj.setId(rs.getInt("_id"));
			obj.setBasicModuleId(rs.getInt("_basic_module_id"));
			obj.setDealerId(rs.getInt("_dealer_id"));
			obj.setEnterpriseId(rs.getInt("_enterprise_id"));
			obj.setAccount(rs.getString("_account"));
			obj.setPwd(rs.getString("_pwd"));
			obj.setName(rs.getString("_name"));
			obj.setOpenId(rs.getString("_open_id"));
			obj.setStatus(rs.getShort("_status"));
			obj.setContact(rs.getString("_contact"));
			obj.setAdditionalContact(rs.getString("_additional_contact"));
			obj.setAudit(rs.getShort("_audit"));
			obj.setRemarks(rs.getString("_remarks"));
			obj.setAcctType(rs.getShort("_acct_type"));
			return obj;
		}
	}
	
	/**是否为分表策略,为true,表名加上传递的企业code*/
	public static final boolean ISSUBMETER = true;
	
	/**
	 *@Title: getEntTableName
	 *@Description: 根据表策略(分库/表)获得表名
	 *@return String
	 *@date 2017年7月13日 上午11:52:48
	 *@param eseCode
	 *@param tName
	 */
	protected String getTname(final String eseCode, final String tName) {
		
		final StringBuilder sb = new StringBuilder();
		if(ISSUBMETER) { // 分表
			if(Utils.isNotEmpty(eseCode)){//企业code不为空
				sb.append("_").append(eseCode);
			}
		}
		sb.append(tName);
		return sb.toString();
	}
	
	
	 
	 /**
	 * 分销app合并分支结束-------------------------------------
	 */
}
